package edu.spec.dengyan.week02.class03;
import java.util.Date;
import java.util.Random;

public class Rand {
    Random rand;
    static int AMOUNT = 200; // 要生成的随机数数量
    int[] numList; // 生成的随机数列表
    int oddAmount; // 奇数数量
    int evenAmount; // 偶数数量
    float oddFreq; // 奇数出现频率
    float evenFreq; // 偶数出现频率
    public Rand() {
        rand = new Random(new Date().getTime()); // 以时间作为随机数生成器的种子
        numList = new int[AMOUNT];
        oddAmount = 0;
        evenAmount = 0;
        oddFreq = 0.0f;
        evenFreq = 0.0f;
    }
    public int getOddAmount() {
        return oddAmount;
    }
    public int getEvenAmount() {
        return evenAmount;
    }
    public float getOddFreq() {
        oddFreq = oddAmount / 200f;
        return oddFreq;
    }
    public float getEvenFreq() {
        evenFreq = evenAmount / 200f;
        return evenFreq;
    }
    /**
     * 生成随机数并放到列表numList中
     */
    void getRandNumbers() {
        int index = 0;
        while (index < AMOUNT) {
            numList[index] = Math.abs(rand.nextInt(900)) + 100;
            index++;
        }
    }
    /**
     * 遍历列表，判断每个元素是奇数还是偶数
     */
    void adjust() {
        int index = 0;
        while (index < AMOUNT) {
            if (numList[index] % 2 != 0) {// 偶数能被2整除，奇数不能被2整除
                oddAmount++;
            } else {
                evenAmount++;
            }
            index++;
        }
    }
    /**
     * 打印出生成的所有随机数
     */
    void print() {
        int index = 1;
        System.out.println("随机数列表");
        while (index <= AMOUNT) {
            if (index % 10 == 0) { // 每行10个数，所以用10作%运算
                System.out.println(numList[index - 1]);
            } else {
                System.out.print(numList[index - 1] + "\t");
            }
            index++;
        }
        System.out.println();
    }
    public static void main(String[] args) {
        Rand r = new Rand();
        r.getRandNumbers();
        r.print();
        r.adjust();
        System.out.println("奇数出现个数：" + r.getOddAmount());
        System.out.println("奇数出现频率：" + r.getOddFreq());
        System.out.println("偶数出现个数：" + r.getEvenAmount());
        System.out.println("奇数出现频率：" + r.getEvenFreq());
    }
}
